#!bin/bash
export PROFILE_PATH=${CYBERTRON_PATH:-'/home/caros/cybertron'}/bin
control_op=$1
output_dir=$2

if [[ "$control_op" = "start" ]]; then
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.py | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -9
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.sh | grep -v 'control' |grep -v 'grep' |awk '{print $2}' | sudo xargs kill -9
  if [[ ! -d $2 ]]; then
      mkdir -p $2
  else
      echo "Directory already exists" $2
  fi

  count=`ps -ef|grep 'mainboard -p compute2d'|grep -v 'grep'| wc -l`
  timeout=10
  while [ $count -lt 1 ];do
      count=`ps -ef|grep 'mainboard -p compute2d'|grep -v 'grep'| wc -l`
      let timeout--
      if [ $timeout -le 0 ]; then
          echo "mainboard主程序启动超时...."
          exit -1
      fi
      sleep 1
  done
  sleep 60
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.py | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -9
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.sh | grep -v 'control' |grep -v 'grep' |awk '{print $2}' | sudo xargs kill -9
  echo "Csec@keyL7256" | sudo -S bash  ${PROFILE_PATH}/profile.sh $2 ${CYBERTRON_PATH} &
  pkill -9 -f -e -u caros "log_speed_analyze.py"
  host_name=$(/usr/local/bin/get_acu_board_role)
  rm -rf /opt/log/anp3/log_speed_*
  python3 ${CYBERTRON_PATH}/bin/log_speed_analyze.py $host_name &
  
elif [[ "$control_op" = "stop" ]]; then
  ps -ef | grep -E "profile.py|profile.sh|profile_cyber_proc.py" | grep -v 'grep'
  [ $? -eq 1 ] && exit 1
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.py | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -2
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.sh | grep -v control | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -2
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile_cyber_proc.py | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -2
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.py | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -9
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile.sh | grep -v control | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -9
  echo "Csec@keyL7256" | sudo -S ps -aux | grep profile_cyber_proc.py | grep -v 'grep' | awk '{print $2}' | sudo xargs kill -9
  echo "Csec@keyL7256" | sudo -S chown caros $2/*
  pkill -9 -f -e -u caros "log_speed_analyze.py"
fi